﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Best.SmartCms.CodeGeneration.BuiderFactory;
using Best.SmartCms.CodeGeneration.Model;
using InputType = System.Windows.Input.InputType;

namespace Best.SmartCms.CodeGeneration
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {

        private ObservableCollection<ColumnFiled> _dt = new ObservableCollection<ColumnFiled>();

        public MainWindow()
        {
            InitializeComponent();
            for (int i = 0; i < 30; i++)
            {
                _dt.Add(new ColumnFiled());
            }

            this.DgData.ItemsSource = _dt;
        }

        /// <summary>
        /// 删除行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDel_Click(object sender, RoutedEventArgs e)
        {
            ((DataRowView)this.DgData.SelectedItem).Row.Delete();
        }

        /// <summary>
        /// 添加行
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            Dispatcher.BeginInvoke(new Action(delegate
            {
                for (int i = 0; i < 10; i++)
                {
                    _dt.Add(new ColumnFiled());
                }
            }));
        }

        /// <summary>
        /// 开始生成
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, RoutedEventArgs e)
        {
            var domainName = TbDomainName.Text;
            if (string.IsNullOrEmpty(domainName))
            {
                MessageBox.Show("请输入DomainName");
                return;
            }

            var isBulderDomain = CbDomain.IsChecked.GetValueOrDefault();
            var isBulderDto = CbDto.IsChecked.GetValueOrDefault(); 
            var isBulderService = CbService.IsChecked.GetValueOrDefault();
            var isBulderJs = CbJs.IsChecked.GetValueOrDefault();

            if (!isBulderDomain && !isBulderDto && !isBulderService && !isBulderJs)
            {
                MessageBox.Show("生成类型必须选择一个");
                return;
            }

            var list = new List<ColumnFiled>();
            list.AddRange(_dt.Where(p=>!string.IsNullOrEmpty(p.Name)).ToList());

            var dto = new CodeGenerationDto
            {
                IsBulderDomain = isBulderDomain,
                IsBulderDto = isBulderDto,
                IsBulderJs = isBulderJs,
                IsBulderService = isBulderService,
                DomainName = domainName,
                ColumnFileds = list
            };

            CodeGenerationFactory factory = new DomainBuilder();
            factory.Excute(dto);

            factory = new DtoBuilder();
            factory.Excute(dto);

            factory = new ServiceBuilder();
            factory.Excute(dto);

            factory = new JsBuilder();
            factory.Excute(dto);

            factory = new ModifyFileBuilder();
            factory.Excute(dto);

            MessageBox.Show("生成成功！");
        }
    }
}
